package com.example.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.Domain.Card;
import com.example.Domain.Inform;
import com.example.Domain.User;
import com.example.Domain.Vodeo;
import com.example.Mapper.CardMapper;
import com.example.Util.JwtUtil;
import com.example.common.ServerResponse;
import com.example.service.Impl.CardServiceImol;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Card")
public class CardController {
    @Autowired
    private CardServiceImol cardall;
    /*卡片查询全部*/
    @RequestMapping("/cardall")
    public ServerResponse cardall(@RequestBody String data, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try{
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println(jsonObject+":jsonObject");
            System.out.println(data+":data");
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Card> a=cardall.cardall(pageNum, pageSize);
            PageInfo<Card> pageInfo = new PageInfo<>(a);
            System.out.println(pageInfo);

            return ServerResponse.createBySuccess("查询成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("查询失败");
        }
    }
    /*数据回显*/
    @RequestMapping("/cardecho")
    public ServerResponse cardecho(HttpServletRequest request,Integer id){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            List<Card> a=cardall.cardecho(id);
            return ServerResponse.createBySuccess("成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }
    /*
     * 添加卡片
     * */
    @RequestMapping("/cardinsert")
    public ServerResponse cardinsert(@RequestBody String Data, HttpServletRequest request,Card card){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject=JSONObject.parseObject(Data);
            String cardName=jsonObject.getString("cardName");
            Date cardTime=jsonObject.getDate("cardTime");
            String cardDescribe=jsonObject.getString("cardDescribe");
            String cardImg=jsonObject.getString("cardImg");
            Integer cardPeople=jsonObject.getInteger("cardPeople");

            Integer id=u.getId();
            card.setUserId(id);
            card.setCardName(cardName);
            card.setCardTime(cardTime);
            card.setCardDescribe(cardDescribe);
            card.setCardImg(cardImg);
            card.setCardPeople(cardPeople);


            System.out.println(card);
            boolean a=cardall.cardinsert(card);

            return ServerResponse.createBySuccess("成功",a);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMsg("失败");
        }

    }
}
